<?php

namespace Database\Seeders;

use App\Models\Picture;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

class PictureSeeder extends Seeder
{
  private $sourcePath;
  private $targetPath;
  private $targetPathPhotos;
  private $targetPathLogos;

  public function __construct()
  {
    $this->sourcePath = base_path('resources/pictures');
    $this->targetPath = base_path('public/pictures');
    $this->targetPathPhotos = $this->targetPath . '/photos';
    $this->targetPathLogos = $this->targetPath . '/logos';
  }

  public function run()
  {

    $sourcePath01 = $this->sourcePath . '/photos/Cisco-Ramon.png';
    $sourcePath02 = $this->sourcePath . '/photos/Barry-Allen.png';
    $sourcePath03 = $this->sourcePath . '/logos/Citizen.png';
    $targetPath01 = $this->targetPathPhotos . '/Cisco-Ramon.png';
    $targetPath02 = $this->targetPathPhotos . '/Barry-Allen.png';
    $targetPath03 = $this->targetPathLogos . '/Citizen.png';

    if (!File::isDirectory($this->targetPath)) {
      File::makeDirectory($this->targetPath);
      Log::info($this->targetPath . ' created!');
    }
    if (!File::isDirectory($this->targetPathPhotos)) {
      File::makeDirectory($this->targetPathPhotos);
      Log::info($this->targetPathPhotos . ' created!');
    }
    if (!File::isDirectory($this->targetPathLogos)) {
      File::makeDirectory($this->targetPathLogos);
      Log::info($this->targetPathLogos . ' created!');
    }
    if (File::exists($targetPath01)) {
      File::delete($targetPath01);
    }
    if (File::exists($targetPath02)) {
      File::delete($targetPath02);
    }
    if (File::exists($targetPath03)) {
      File::delete($targetPath03);
    }
    File::copy($sourcePath01, $targetPath01);
    File::copy($sourcePath02, $targetPath02);
    File::copy($sourcePath03, $targetPath03);

    $picture01 = new Picture();
    $picture01->user_id = 1;
    $picture01->type_id = 12;
    $picture01->title = 'La foto di Cisco';
    $picture01->filename = $targetPath01;
    $picture01->original_filename = $sourcePath01;
    $picture01->mime_type = File::mimeType($sourcePath01);
    $picture01->size = File::size($sourcePath01);
    $picture01->active = true;
    $picture01->save();

    $picture02 = new Picture();
    $picture02->user_id = 2;
    $picture02->type_id = 12;
    $picture02->title = 'La foto di Barry';
    $picture02->filename = $targetPath02;
    $picture02->original_filename = $sourcePath02;
    $picture02->mime_type = File::mimeType($sourcePath02);
    $picture02->size = File::size($sourcePath02);
    $picture02->active = true;
    $picture02->save();

    $picture03 = new Picture();
    $picture03->user_id = 3;
    $picture03->type_id = 13;
    $picture03->title = 'Il logo del Citizen';
    $picture03->filename = $targetPath03;
    $picture03->original_filename = $sourcePath03;
    $picture03->mime_type = File::mimeType($sourcePath03);
    $picture03->size = File::size($sourcePath03);
    $picture03->active = true;
    $picture03->save();
  }
}
